package com.example.lib_core.mvp.model;

import com.example.lib_core.http.BaseObserver;
import com.example.lib_core.http.RetrofitManager;


import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;

public class BaseModel implements IModel{
    private List<BaseObserver> observers=new ArrayList<>();
    protected Retrofit getNetWorkRetrofit(){
         return RetrofitManager.getInstance().getRetrofit();
    };

    //通过网络获取数据
    protected void RequestNetwork(Observable<?> observable, BaseObserver baseObserver){
        if(observable!=null){
            observable.subscribeOn(Schedulers.io()).
                    observeOn(AndroidSchedulers.mainThread()).
            subscribe(baseObserver);
            observers.add(baseObserver);
        }
    };

    //通过数据库获取数据
    public void RequestDB(){};

    //上传
    public void updata(){};


    @Override
    public void onDestroy() {
        if(observers.size()>0){
            for (BaseObserver observer : observers) {
                if(observer.isDisposed()){
                    observer.dispose();
                }
            }
            observers.clear();
        }

    }
}
